package wec_wallet.ebo.com.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by admin on 2018/4/17.
 */

@SuppressLint("AppCompatCustomView")
public class WaterImageView extends ImageView {
    public WaterImageView(Context context) {
        super(context);
    }

    public WaterImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public WaterImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @SuppressLint("NewApi")
    public WaterImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void startChangeAlpha(int startAlpha){
        handlerAlpha.sendEmptyMessage(1);
        alpha = startAlpha;
    }

    int alpha = 255;
    int alpha_speed = 12;
    boolean alphaAdd;
    Handler handlerAlpha = new Handler() {
        @Override
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    if (alphaAdd) {
                        alpha+=alpha_speed;
                        if(alpha>=255){
                            alpha=255;
                            alphaAdd = false;
                        }
                    } else {
                        alpha-=alpha_speed;
                        if(alpha<=20){
                            alpha=20;
                            alphaAdd = true;
                        }
                    }
                    setAlpha(alpha);
                    sendEmptyMessageDelayed(1,200);
                    break;
            }
        }
    };

    @Override
    public void destroyDrawingCache() {
        super.destroyDrawingCache();
        handlerAlpha = null;
    }
}
